define(function (require, exports, module) {
    var oShadow = $('.js_shadow');				//阴影
    var oHeader = $('.js_headerBox');
    var oFooter = $('.js_footer');              //底部
    var oGoTop = $('.js_goTop');                //回到顶部
    var oSearchBox = $('.js_searchBox');       	//搜索部分
    var oSearchTxt = $('.js_searchTxt');        //搜索框
    var oSearchClear = $('.js_clearBtn');       //搜索框清空
    var maxLWord = $('.js_wordmax');            //最大字数
    var oUserMessage = $('.js_message').find('textarea');  //留言区域
    var oLoadingPage = $('.js_loadingPage');

    var $window = $(window);
    var oSideBox = $('.js_sideBox');		//侧边按钮框
    var oContain = $('.js_contain');

    var resizeTimer = null;
    var textPage = oHeader.find('.publicHeadTit p').text();

    // 检测app隐藏顶部返回条
    JudgeAppHide(function () {
        // IOS
        HideHeadTag();
    }, function () {
        // Android
        HideHeadTag();
    }, function () {
        // H5
        if(isWeiXin()) {
            HideHeadTag();
        }
    });


    // if(isIphoneX()) {
    //    oFooter.css({
    // 		'padding-bottom': '38px',
    // 		'background-color': '#fff'
    //	})
    // }

    oContain.css({
        'padding-top': oHeader.outerHeight(true),
        'padding-bottom': oFooter.outerHeight(true)
    });


    $window.on('resize', function () {
        if (resizeTimer) {
            clearTimeout(resizeTimer);
        }
        resizeTimer = setTimeout(function () {
            oContain.css({
                'padding-top': oHeader.outerHeight(true),
                'padding-bottom': oFooter.outerHeight(true)
            });
        }, 200)
    });

    oShadow.on('click', function (e) {
        e.stopPropagation()
    });

    WindowScroll()
    $window.on('scroll', function (e) {
        throttle(WindowScroll, window);
    });

    oSearchBox.on("touchmove", function (e) {
        e.preventDefault()
    });
    oFooter.on("touchmove", function (e) {
        e.preventDefault()
    });
    oLoadingPage.on('touchmove', function () {
        return false
    });
    oShadow.on("touchmove", function (e) {
        e.preventDefault()
    });

    // 回到顶部
    oGoTop.on('click', function (e) {
        $window.scrollTop(0)
        e.stopPropagation()
    });

    // 清空搜索框  oSearchClear
    oSearchClear.on('click', function (e) {
        oSearchTxt.val('');
        $(this).hide().siblings('.js_Text').val('').siblings('.js_error').hide().removeClass('on');
        e.stopPropagation();
    });

    // 显示清空按钮
    oSearchTxt.on('input propertychange', function () {
        $(this).val() == '' ? oSearchClear.hide() : oSearchClear.show();
    });
    // 显示清空按钮(获取焦点)
    oSearchTxt.on('focus', function () {
        $(this).val() == '' ? oSearchClear.hide() : oSearchClear.show();
    });

    oSearchTxt.on('blur', function () {
        $(this).val() == '' ? oSearchClear.hide() : oSearchClear.show();
    });

    // 限制字数
    if (oUserMessage.length > 0) {
        statInputNum(oUserMessage, maxLWord);
    }

    // 页面滚动
    function WindowScroll() {
        var scrollTime;
        var _disY = $(window).scrollTop();

        if (window.location.pathname.search('h/store/normalStore') > 0) {
            oSideBox.stop().animate({'right': 0}, 250);
        }
        else {
            _disY > 300 ? oSideBox.stop().animate({'right': 0}, 250) : oSideBox.stop().animate({'right': '-3.5rem'}, 250);
        }
    }


    // 购物车标记数量
    var oUserNum = $('.js_userNum');
    if (oUserNum.length > 0) {
        //购物车标记
        $.ajax({
            type: "POST",
            url: "/shop/cartItem/getCount",
            dataType: "json",
            success: function (data) {
                if (data.count != 0) {
                    oUserNum.show();
                    oUserNum.html(data.count);
                } else {
                    oUserNum.hide();
                }
            },
            error: function (textStatus) {
                if (textStatus.status == 403) {
                    oUserNum.hide();
                }
            },
            complete: function (XHR) {
                XHR = null;
            }
        });
    }


    //节流控制
    function throttle(method, context, time) {
        time == undefined ? time = 200 : time = time;
        //console.log(time)
        clearTimeout(method.tId);
        method.tId = setTimeout(function () {
            method.call(context);
            //console.log(111)
        }, time);
    }

    /* 字数统计 */
    function statInputNum(textArea, numItem) {
        var max = [];
        var curLength = [];
        textArea.each(function (i) {
            max[i] = parseInt(numItem.eq(i).text());

            if (textArea.eq(i).val().length > 0) {
                curLength[i] = textArea.eq(i).val().length;
                numItem.eq(i).empty().text(max[i] - curLength[i]);
            }
            textArea.eq(i).attr("maxlength", max[i]).on('input propertychange', function () {
                curLength[i] = $(this).val().length;
                numItem.eq(i).empty().text(max[i] - curLength[i]);
            });
        });

    }


    // 检测 安卓 ios 隐藏指定对象
    function JudgeAppHide(ios, and, h5) {
        var u = navigator.userAgent;
        //var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        // 检测室安卓还是ios
        if (u.search('origin/sfddjapp') > 0) {
            if (isIOS) {
                if (ios) ios();
            }
            else {
                if (and) and();
            }
        }

        // 检测ddj
        else {
            if (h5) h5();
        }
    }

    function isIphoneX() {
        return /iphone/gi.test(navigator.userAgent) && (screen.height == 812 && screen.width == 375)
    }

    function HideHeadTag() {
        // 防止隐藏首页顶部
        if (oHeader.find('.publicHead').length > 0 && !oHeader.find('.publicHead').hasClass('noHide') ) {
            oHeader.remove();
            document.title = textPage;
        }
    }

    function isWeiXin() {
        var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 判断是否是支付宝浏览器
     * @returns {boolean}
     */
    function isAlipayClient() {
        var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/AlipayClient/i) == 'alipayclient') {
            return true;
        } else {
            return false;
        }
    }

});


